package com.ruoyi.incident.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.incident.service.IXlkjIncidentService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.DictStrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 事件Controller
 *
 * @author wang
 * @date 2022-01-13
 */
@RestController
@RequestMapping("/app/incident/incident")
public class XlkjIncidentControllerApp extends BaseController
{
    @Autowired
    private IXlkjIncidentService xlkjIncidentService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询事件列表
     */
//    @PreAuthorize("@ss.hasPermi('incident:incident:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjIncident xlkjIncident)
    {
        startPage();
        if(xlkjIncident.getDepartmentId() != null && xlkjIncident.getDepartmentId() != ""){
            List<String> list = sysDeptMapper.selectIdByParentId(Long.valueOf(xlkjIncident.getDepartmentId()));
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            xlkjIncident.setDepartmentList(str);
        }

        if(xlkjIncident.getStatus().equals("1")){
            xlkjIncident.setStatus(null);
            xlkjIncident.setAaa("aaa");
        }

        List<XlkjIncident> list = xlkjIncidentService.selectXlkjIncidentList(xlkjIncident);
        for(XlkjIncident incident:list){
            //部门值翻译
            String deptName = sysDeptService.selectDeptById(Long.valueOf(incident.getDepartmentId())).getDeptName();
            incident.setDepartmentId(deptName);
            //字典翻译
            incident.setType(DictUtils.getDictLabel(DictStrUtils.INCIDENT_TYPE,incident.getType()));
        }
        return getDataTable(list);
    }

    /**
     * 导出事件列表
     */
//    @PreAuthorize("@ss.hasPermi('incident:incident:export')")
//    @Log(title = "事件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjIncident xlkjIncident)
    {
        List<XlkjIncident> list = xlkjIncidentService.selectXlkjIncidentList(xlkjIncident);
        ExcelUtil<XlkjIncident> util = new ExcelUtil<XlkjIncident>(XlkjIncident.class);
        return util.exportExcel(list, "事件数据");
    }

    /**
     * 获取事件详细信息
     */
//    @PreAuthorize("@ss.hasPermi('incident:incident:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        XlkjIncident incident = xlkjIncidentService.selectXlkjIncidentById(id);
        //字典值翻译
        incident.setQuestionType(DictUtils.getDictLabel(DictStrUtils.QUESTION_TYPE,incident.getQuestionType()));
        incident.setRegionType(DictUtils.getDictLabel(DictStrUtils.REGION_TYPE,incident.getRegionType()));
        incident.setRiskType(DictUtils.getDictLabel(DictStrUtils.RISK2_TYPE,incident.getRiskType()));
        return AjaxResult.success(incident);
    }

    /**
     * 新增事件
     */
//    @PreAuthorize("@ss.hasPermi('incident:incident:add')")
//    @Log(title = "事件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjIncident xlkjIncident)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(xlkjIncident.getPhone());
        xlkjIncident.setUserId(sysUser.getUserId());
        //设置默认状态
        xlkjIncident.setStatus("1");
        return toAjax(xlkjIncidentService.insertXlkjIncident(xlkjIncident));
    }

    /**
     * 修改事件
     */
//    @PreAuthorize("@ss.hasPermi('incident:incident:edit')")
//    @Log(title = "事件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjIncident xlkjIncident)
    {
        return toAjax(xlkjIncidentService.updateXlkjIncident(xlkjIncident));
    }

    /**
     * 删除事件
     */
//    @PreAuthorize("@ss.hasPermi('incident:incident:remove')")
//    @Log(title = "事件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjIncidentService.deleteXlkjIncidentByIds(ids));
    }
}
